/*
 * Copyright (c) 2025 CAX Conversion Project
 */
package com.comac.ins.isfd.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.comac.ins.common.core.domain.R;
import com.comac.ins.common.core.validate.AddGroup;
import com.comac.ins.common.core.validate.EditGroup;
import com.comac.ins.common.excel.utils.ExcelUtil;
import com.comac.ins.common.idempotent.annotation.RepeatSubmit;
import com.comac.ins.common.log.annotation.Log;
import com.comac.ins.common.log.enums.BusinessType;
import com.comac.ins.common.mybatis.core.page.PageQuery;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.common.web.core.BaseController;
import com.comac.ins.isfd.constant.enums.IsfdStructureDefinitionTreeLevelEnum;
import com.comac.ins.isfd.domain.bo.IsfdModelManagementStructureDefinitionTreeBo;
import com.comac.ins.isfd.domain.vo.IsfdModelManagementStructureDefinitionTreeNodeVo;
import com.comac.ins.isfd.domain.vo.IsfdModelManagementStructureDefinitionTreeRemoveCheckVo;
import com.comac.ins.isfd.domain.vo.IsfdModelManagementStructureDefinitionTreeVo;
import com.comac.ins.isfd.service.IIsfdModelManagementStructureDefinitionTreeService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 模型管理-模型库结构定义树controller
 *
 * @author Lion Li
 * @date 2024-08-13
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/isfd/modelManagementStructureDefinitionTree")
public class IsfdModelManagementStructureDefinitionTreeController extends BaseController {

    private final IIsfdModelManagementStructureDefinitionTreeService IsfdModelManagementStructureDefinitionTreeService;

    /**
     * 查询【模型库结构定义树】列表-平铺结构
     */
    @SaCheckPermission("isfd:modelManagementStructureDefinitionTree:list")
    @GetMapping("/list")
    public TableDataInfo<IsfdModelManagementStructureDefinitionTreeVo> list(IsfdModelManagementStructureDefinitionTreeBo bo, PageQuery pageQuery) {
        return IsfdModelManagementStructureDefinitionTreeService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询【根据机型查询批次】列表
     */
    @SaCheckPermission("isfd:modelManagementStructureDefinitionTree:list")
    @GetMapping("/batchNoList")
    public R<List<IsfdModelManagementStructureDefinitionTreeVo>> batchNoList(@RequestParam(value = "modelNumber") String modelNumber) {
        IsfdModelManagementStructureDefinitionTreeVo modelVo = IsfdModelManagementStructureDefinitionTreeService.queryOneByQueryIndex(modelNumber, null, null);
        IsfdModelManagementStructureDefinitionTreeBo bo = new IsfdModelManagementStructureDefinitionTreeBo();
        bo.setParentId(modelVo.getId());
        bo.setLevel(IsfdStructureDefinitionTreeLevelEnum.BATCH_NO.getValue());
        return R.ok(IsfdModelManagementStructureDefinitionTreeService.queryList(bo));
    }

    /**
     * 查询【模型库结构定义树】列表-树结构
     */
    @SaCheckPermission("isfd:modelManagementStructureDefinitionTree:list")
    @GetMapping("/listTree")
    public R<List<IsfdModelManagementStructureDefinitionTreeNodeVo>> listTree() {
        return R.ok(IsfdModelManagementStructureDefinitionTreeService.queryListTree());
    }


    /**
     * 导出【模型库结构定义树】列表
     */
    @SaCheckPermission("isfd:modelManagementStructureDefinitionTree:export")
    @Log(title = "【模型库结构定义树】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(IsfdModelManagementStructureDefinitionTreeBo bo, HttpServletResponse response) {
        List<IsfdModelManagementStructureDefinitionTreeVo> list = IsfdModelManagementStructureDefinitionTreeService.queryList(bo);
        ExcelUtil.exportExcel(list, "【模型库结构定义树】", IsfdModelManagementStructureDefinitionTreeVo.class, response);
    }

    /**
     * 获取【模型库结构定义树】详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("isfd:modelManagementStructureDefinitionTree:query")
    @GetMapping("/{id}")
    public R<IsfdModelManagementStructureDefinitionTreeVo> getInfo(@NotNull(message = "主键不能为空")
                                                                   @PathVariable Long id) {
        return R.ok(IsfdModelManagementStructureDefinitionTreeService.queryById(id));
    }

    /**
     * 新增【模型库结构定义树】
     */
    @SaCheckPermission("isfd:modelManagementStructureDefinitionTree:add")
    @Log(title = "【模型库结构定义树】", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody IsfdModelManagementStructureDefinitionTreeBo bo) {
        return toAjax(IsfdModelManagementStructureDefinitionTreeService.insertByBo(bo));
    }

    /**
     * 修改【模型库结构定义树】
     */
    @SaCheckPermission("isfd:modelManagementStructureDefinitionTree:edit")
    @Log(title = "【模型库结构定义树】", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody IsfdModelManagementStructureDefinitionTreeBo bo) {
        return toAjax(IsfdModelManagementStructureDefinitionTreeService.updateByBo(bo));
    }

    /**
     * 删除
     */
    @SaCheckPermission("isfd:modelManagementStructureDefinitionTree:remove")
    @Log(title = "【模型库结构定义树】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(IsfdModelManagementStructureDefinitionTreeService.deleteWithValidByIds(List.of(ids)));
    }

    /**
     * 删除-检查
     */
    @SaCheckPermission("isfd:modelManagementStructureDefinitionTree:remove")
    @Log(title = "【模型库结构定义树】", businessType = BusinessType.DELETE)
    @GetMapping("/remove/check")
    public R<IsfdModelManagementStructureDefinitionTreeRemoveCheckVo> removeCheck(@RequestParam(value = "id") Long id) {
        return R.ok(IsfdModelManagementStructureDefinitionTreeService.removeCheck(id));
    }
}
